home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / THREEDOM.ZIP / WINLIB / TEST.C next >
Encoding:
C/C++ Source or Header  |  1996-08-27  |  8.4 KB  |  328 lines

  1. /* @(#)test.c 1.1   8/27/96 */
  2.  
  3. /******************************************************************************
  4. * WinLib: a simple Windows 95 GUI library                                     *
  5. * (C) Copyright 1996 by Philip Stephens                                       *
  6. * (C) Copyright 1996 by the IBM Corporation                                   *
  7. * All Rights Reserved                                                         *
  8. *                                                                             *
  9. * Permission to use, copy, modify, and distribute this software and its       *
  10. * documentation without fee for any non-commerical purpose is hereby granted, *
  11. * provided that the above copyright notice appears on all copies and that     *
  12. * both that copyright notice and this permission notice appear in all         *
  13. * supporting documentation.                                                   *
  14. *                                                                             *
  15. * NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY  *
  16. * PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.       *
  17. * NEITHER PHILIP STEPHENS OR IBM SHALL BE LIABLE FOR ANY DAMAGES SUFFERED BY  *
  18. * THE USE OF THIS SOFTWARE.                                                   *
  19. ******************************************************************************/
  20.  
  21.  
  22. #define STRICT
  23. #define WIN32_LEAN_AND_MEAN
  24. #include <windows.h>
  25. #include <windowsx.h>
  26. #include <commdlg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "winlib.h"
  30. #include "test.h"
  31.  
  32. /*
  33.  * I don't care to hear about parameters or local variables that
  34.  * aren't used in a function.
  35.  */
  36. #pragma warn -aus
  37. #pragma warn -par
  38.  
  39. static HWND main_window;
  40. static HWND bitmap_window;
  41. static HWND status_window;
  42. static fast_bitmap *bitmap_ptr;
  43. static RGBQUAD palette[256];
  44.  
  45. /*************************************
  46.  * Function to create bitmap window. *
  47.  *************************************/
  48.  
  49. void
  50. create_bitmap_window(HWND window_handle)
  51. {
  52.     int index, row, col;
  53.  
  54.     /*
  55.      * Fill in the palette with shades of red.  We don't bother filling in
  56.      * the first and last 10 entries because they won't be used.
  57.      */
  58.     for (index = 30; index < 246; index++) {
  59.         palette[index].rgbRed = (BYTE)index;
  60.         palette[index].rgbGreen = 0;
  61.         palette[index].rgbBlue = 0;
  62.     }
  63.  
  64.     /*
  65.      * Create the bitmap.
  66.      */
  67.     bitmap_ptr = CreateFastBitmap(window_handle, 256, 256,
  68.                       (RGBQUAD *)&palette, 216, 30);
  69.  
  70.     /*
  71.      * Fill in the bitmap with a 16x16 pattern that displays all 256
  72.      * colours in the palette.
  73.      */
  74.     for (row = 0; row < 256; row++)
  75.         for (col = 0; col < 256; col++)
  76.             bitmap_ptr->bits[row * 256 + col] = (BYTE)(((row / 16)
  77.                                 << 4) + (col / 16));
  78. }
  79.  
  80. /**************************************
  81.  * Function to destroy bitmap window. *
  82.  **************************************/
  83.  
  84. void
  85. destroy_bitmap_window(HWND window_handle)
  86. {
  87.     DestroyFastBitmap(bitmap_ptr);
  88. }
  89.  
  90. /************************************
  91.  * Function to paint bitmap window. *
  92.  ************************************/
  93.  
  94. /*
  95.  * IMPORTANT NOTE: It is absolutely essential that you use BeginPaint() and
  96.  * EndPaint() in a paint procedure, otherwise Windows won't call the paint
  97.  * procedures for the remaining windows affected by a WM_PAINT request!
  98.  * Don't ask me *why* this is the case!!  It had me confused for a hell of
  99.  * a long time, wondering why my standard controls weren't been repainted
  100.  * after maximising the main window.  It was all because I'd left out the
  101.  * BeginPaint() and EndPaint() calls in this paint procedure!!
  102.  */
  103. void
  104. paint_bitmap_window(HWND window_handle)
  105. {
  106.     HDC dc;
  107.     PAINTSTRUCT paint_struct;
  108.  
  109.     dc = BeginPaint(window_handle, &paint_struct);
  110.  
  111.     DisplayFastBitmap(bitmap_ptr);
  112.  
  113.     EndPaint(window_handle, &paint_struct);
  114. }
  115.  
  116. /**********************************************
  117.  * Process a char event in the bitmap window. *
  118.  **********************************************/
  119.  
  120. void
  121. process_char_event(HWND window_handle, UINT ch, int repeat)
  122. {
  123.     DisplayMessage(status_window, "Key '%c' was entered\r\n", ch);
  124. }
  125.  
  126. /************************
  127.  * Process a key event. *
  128.  ************************/
  129.  
  130. void
  131. process_key_event(HWND window_handle, UINT keycode, BOOL keydown, int repeat,
  132.           UINT flags)
  133. {
  134.     if (keydown)
  135.         DisplayMessage(status_window,
  136.                    "Key with code %d was pressed\r\n", keycode);
  137.     else
  138.         DisplayMessage(status_window,
  139.                    "Key with code %d was released\r\n", keycode);
  140. }
  141.  
  142. /*************************
  143.  * Process button event. *
  144.  *************************/
  145.  
  146. void
  147. process_button_event(HWND window_handle, UINT message, int x, int y,
  148.              UINT flags)
  149. {
  150.     switch(message) {
  151.     case WM_LBUTTONDOWN:
  152.         DisplayMessage(status_window, "Left button was pressed\r\n");
  153.         break;
  154.     case WM_LBUTTONUP:
  155.         DisplayMessage(status_window, "Left button was released\r\n");
  156.         break;
  157.     case WM_LBUTTONDBLCLK:
  158.         DisplayMessage(status_window,
  159.                    "Left button was double clicked\r\n");
  160.         break;
  161.     case WM_RBUTTONDOWN:
  162.         DisplayMessage(status_window, "Right button was pressed\r\n");
  163.         break;
  164.     case WM_RBUTTONUP:
  165.         DisplayMessage(status_window, "Right button was released\r\n");
  166.         break;
  167.     case WM_RBUTTONDBLCLK:
  168.         DisplayMessage(status_window,
  169.                    "Right button was double clicked\r\n");
  170.         break;
  171.     }
  172. }
  173.  
  174. /***************************************************************
  175.  * Function to throw away a button event in the status window. *
  176.  ***************************************************************/
  177. void
  178. throwaway_button_event(HWND window_handle, UINT message, int x, int y,
  179.                UINT flags)
  180. {
  181. }
  182.  
  183. /***************************************
  184.  * Function to create the main window. *
  185.  ***************************************/
  186.  
  187. /*
  188.  * Callback list for bitmap window.
  189.  */
  190. static event_callbacks bitmap_callback_list = {
  191.     create_bitmap_window,
  192.     destroy_bitmap_window,
  193.     paint_bitmap_window,
  194.     NULL,
  195.     NULL,
  196.     process_button_event,
  197.     NULL,
  198.     NULL,
  199.     NULL
  200. };
  201.  
  202. /*
  203.  * Callback list for status window.
  204.  */
  205. static event_callbacks status_callback_list = {
  206.     NULL,
  207.     NULL,
  208.     NULL,
  209.     NULL,
  210.     NULL,
  211.     process_button_event,
  212.     NULL,
  213.     NULL,
  214.     NULL
  215. };
  216.  
  217. void
  218. create_main_window(HWND window_handle)
  219. {
  220.     bitmap_window = CreateChildWindow(window_handle, 10, 10, 256, 256,
  221.                       WS_BORDER, &bitmap_callback_list);
  222.     status_window = CreateEditControl(window_handle, 280, 60, 300, 206,
  223.                       WS_BORDER | ES_READONLY |
  224.                       ES_WANTRETURN, &status_callback_list);
  225. }
  226.  
  227. /****************************************
  228.  * Function to destroy the main window. *
  229.  ****************************************/
  230.  
  231. void
  232. destroy_main_window(HWND window_handle)
  233. {
  234.     PostQuitMessage(0);
  235. }
  236.  
  237. /**********************************
  238.  * Function to paint main window. *
  239.  **********************************/
  240.  
  241. void
  242. paint_main_window(HWND window_handle)
  243. {
  244.     HDC dc;
  245.     PAINTSTRUCT paint_struct;
  246.     char *title = "A sample Windows 95 application";
  247.     char *author = "(C) Copyright 1996 by Philip Stephens";
  248.  
  249.     dc = BeginPaint(window_handle, &paint_struct);
  250.  
  251.     SetBkMode(dc, TRANSPARENT);
  252.     SetTextColor(dc, RGB(255, 0, 0));
  253.     TextOut(dc, 280, 10, title, strlen(title));
  254.     TextOut(dc, 280, 30, author, strlen(author));
  255.  
  256.     EndPaint(window_handle, &paint_struct);
  257. }
  258.  
  259. /***********************************************
  260.  * Function to handle gaining of window focus. *
  261.  ***********************************************/
  262.  
  263. BOOL
  264. handle_window_focus(HWND window_handle)
  265. {
  266.     SetFocus(window_handle);
  267.     return(InstallFastBitmapPalette(bitmap_ptr));
  268. }
  269.  
  270. /*************************************************
  271.  * Function to process a command from a control. *
  272.  *************************************************/
  273.  
  274. void
  275. process_command_event(HWND window_handle, int menu_id, HWND control_handle,
  276.               UINT code_notify)
  277. {
  278.     char *file_name;
  279.  
  280.     switch(menu_id) {
  281.     case CM_OPEN:
  282.         if ((file_name = OpenFileDialog("Open Threedom world",
  283.                  "Threedom world (*.w3d)\0*.w3d\0")) != NULL)
  284.             MessageBox(NULL, file_name, "Info", MB_OK);
  285.         break;
  286.     case CM_EXIT:
  287.         PostQuitMessage(0);
  288.         break;
  289.     }
  290. }
  291.  
  292. /*****************************
  293.  * Main program entry point. *
  294.  *****************************/
  295.  
  296. /*
  297.  * List of event function handlers for main window.
  298.  */
  299. static event_callbacks callback_list = {
  300.     create_main_window,
  301.     destroy_main_window,
  302.     paint_main_window,
  303.     process_char_event,
  304.     process_key_event,
  305.     process_button_event,
  306.     NULL,
  307.     handle_window_focus,
  308.     process_command_event
  309. };
  310.  
  311. void
  312. test_proc(void)
  313. {
  314. }
  315.  
  316. int WINAPI
  317. WinMain(HINSTANCE instance, HINSTANCE previnst, LPSTR comand_line,
  318.           int window_state)
  319. {
  320.     WinLibInit(instance);
  321.     main_window = CreateMainWindow("Test Application", 600, 320,
  322.                        window_state, "menu", &callback_list);
  323.     if (main_window)
  324.         EnterEventLoop(test_proc);
  325.     WinLibCleanUp();
  326.     return(TRUE);
  327. }
  328.